package com.ht.api.call.func.impl.sys;

import java.util.HashMap;
import java.util.Map;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 规则模板详情查询
 * @author asq
 * @createTime 2025年3月19日 09:38:26
 */
public class RuleTemplateInfoQueryFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "规则模板详情查询";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		String code = param.getStr("code");
        String sql = """
                SELECT
                    r.main_id,
                	r.rule_group_id,
                	r.rule_key_id,
                	r.order_num ,
                	k.name,
                	k.column_id,
                	k.value_type,
                	k.`values`,
                	k.mapping_id,
                	k.relation_config,
                	k.is_relation
                FROM
                	rule_group_relation r
                	LEFT JOIN rule_group g ON r.rule_group_id = g.main_id
                	AND g.template_code = ?
                	LEFT JOIN rule_key k ON r.rule_key_id = k.main_id
                WHERE r.sys_deleted = 1 
                ORDER BY
                	g.order_num ASC,
                	r.order_num ASC
                """;

        DbList list = Db.query(sql, code);

        DbList groupList = Db.createSqlParser("rule_group").where("template_code", code).query();
        Map<String, JSONObject> groupMap = new HashMap<>();
        for (Entity group : groupList) {
            JSONArray keys = JSONUtil.createArray();
            group.set("keys", keys);
            groupMap.put(group.getMainId(), group);
        }


        for (Entity entity : list) {
            String groupId = entity.getStr("rule_group_id");
            if (groupMap.containsKey(groupId)) {
                JSONObject ruleKey = JSONUtil.createObj();
                ruleKey.set("main_id", entity.get("main_id"));
                ruleKey.set("rule_key_id", entity.get("rule_key_id"));
                ruleKey.set("rule_group_id", entity.get("rule_group_id"));
                ruleKey.set("order_num", entity.get("order_num"));
                ruleKey.set("name", entity.get("name"));
                ruleKey.set("column_id", entity.get("column_id"));
                ruleKey.set("value_type", entity.get("value_type"));
                ruleKey.set("values", entity.get("values"));
                ruleKey.set("mapping_id", entity.get("mapping_id"));
                ruleKey.set("relation_config", entity.get("relation_config"));
                ruleKey.set("is_relation", entity.get("is_relation"));

                JSONObject group = groupMap.get(groupId);
                JSONArray keys = group.getJSONArray("keys");
                keys.add(ruleKey);
            }
        }

        JSONArray ruleTemplateInfo = JSONUtil.createArray();
        for (String s : groupMap.keySet()) {
            ruleTemplateInfo.add(groupMap.get(s));
        }
		
		return JSONUtil.createObj().set("info", ruleTemplateInfo);
	}

}
